package com.game.tools.exception;

import com.game.pojo.vo.Result;
import com.game.tools.exception.CustomException;
import com.game.tools.exception.ExceptionEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常拦截
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandle {

    /**
     * 自定义异常
     */
    @ExceptionHandler(value = CustomException.class)
    public Result processException(CustomException e){
        log.error("位置：" + e.getMethod() + " -> 错误信息：" + e.getLocalizedMessage());
        return new Result(e.getCode(), ExceptionEnum.getByCode(e.getCode()).getMsg(), e.getLocalizedMessage());
    }

    /**
     * 通用异常
     * @param e
     * @return
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(Exception.class)
    public Result exception(Exception e){
        e.printStackTrace();
        return new Result(ExceptionEnum.UNKNOWN_ERROR.getCode(), ExceptionEnum.UNKNOWN_ERROR.getMsg(), e.getLocalizedMessage());
    }
}
